package com.shoe.controller;

import com.shoe.pojo.ShoeLimit;
import com.shoe.service.ShoeLoginService;
import com.shoe.service.ShoeUserService;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.zip.CheckedOutputStream;

@Controller
@CrossOrigin
public class LoginController {

    @Autowired
    ShoeLoginService shoeLoginService;
    @Autowired
    ShoeUserService shoeUserService;


    /*@PostMapping("home")
    public String home(){
        // forward:/ 加不加 /的区别
        return "user/home";
    }*/

    @PostMapping("login")
    public String selectLogin(String name, String pwd, HttpServletRequest request, HttpServletResponse response) {
        ResponseResult responseResult = null;
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("loginpwd")) {
                    responseResult = shoeLoginService.selectLoginByRemember(name, cookies[i].getValue());
                    break;
                }
            }
        }
        if (responseResult == null || responseResult.getCode() == 3) {
            ShoeLimit shoeLimit = shoeLoginService.selectLogin(name, pwd);
            if (shoeLimit != null) {
                HttpSession session = request.getSession();
                session.setAttribute("uname", name);
                Cookie cookie = new Cookie("uname", name);
                cookie.setMaxAge(7 * 24 * 60 * 60* 60);
                response.addCookie(cookie);
                Cookie lpd = new Cookie("loginpwd", pwd);
                cookie.setMaxAge(7 * 24 * 60 * 60* 60);
                response.addCookie(lpd);

                System.out.println(name);

                Long a = shoeUserService.selectIdd(name);
                System.out.println(a);

                String b = String.valueOf(a);

                Cookie cookieId = new Cookie("userid", b);
                cookie.setMaxAge(7 * 24 * 60 * 60 * 60);
                response.addCookie(cookieId);
                /*Cookie cookieId = new Cookie();
                cookie.setMaxAge(7 * 24 * 60 * 60);
                response.addCookie(cookieId);*/
                return "redirect:http://localhost:63343/qiguai/html/header-2.html";
            } else {
                return "redirect:http://localhost:63343/qiguai/html/login.html";
            }
        }
        return "redirect:http://localhost:63343/qiguai/html/header-2.html?_ijt=efd90n9ot6nh4d8h7na4o1i8a3";
    }






    @RequestMapping("selectName")
    @ResponseBody
    public ResponseResult selectUserName(@RequestBody ShoeLimit shoeLimit){
        /*System.out.println(shoeLimit.getUserName());*/
        return shoeLoginService.selectUserName(shoeLimit.getUserName());
    }

    @RequestMapping("selectPwd")
    @ResponseBody
    public ResponseResult selectPassWord(@RequestBody ShoeLimit shoeLimit){
        /*System.out.println(shoeLimit.getUserPassword());*/
        return shoeLoginService.selectPassWord(shoeLimit.getUserPassword());
    }

    @RequestMapping("add")
    public String addUser(@RequestParam String uname,String pwd,String power){
        System.out.println("2222");
        ShoeLimit shoeLimit = new ShoeLimit();
        shoeLimit.setUserName(uname);
        shoeLimit.setUserPassword(pwd);
        shoeLimit.setUserPower(power);
        int a = shoeLoginService.addUser(shoeLimit);
        return "redirect:http://localhost:63343/qiguai/html/login.html";
    }

    @RequestMapping("update")
    public String update(String name, String pwd,String newpwd){
        ShoeLimit shoeLimit = shoeLoginService.selectLogin(name,pwd);
        shoeLimit.setUserPassword(newpwd);
        if (shoeLimit != null){
            int a = shoeLoginService.update(shoeLimit);
            System.out.println(a);
            return "redirect:http://localhost:63343/qiguai/html/login.html?_ijt=fo0hq7t3fup9rk0umfdv1m6bfm";
        }
        return "redirect:http://localhost:63343/qiguai/html/xiugaimma.html?_ijt=fo0hq7t3fup9rk0umfdv1m6bfm";
    }

    @RequestMapping("adminLogin")
    @ResponseBody
    public String Login(String userName, String passWord){
        ResponseResult responseResult = new ResponseResult();
        if ("admin".equals(userName) && "123456".equals(passWord)){
            responseResult.setCode(0);
            responseResult.setMsg("登录成功");
            return "成功";
        }
        return "失败";
    }
}
